//
//  API_Login.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import Moya

enum API_Login {
    /// 苹果登录
    case appleLogin(String, String)
    /// FB登录
    case facebookLogin(String, String, String, String, String)
    /// GG登录
    case googleLogin(String, String, String, String, String)
    /// 埋点
    case events(String, Double)
    /// 阅读时长上报
    case readReport(String, Int)
    ///
    case checkUser
    /// 剪切板
    case pasteboard(String)
    /// 归因
    case attribution(String, String)

}

let login_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Login>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_login = MoyaProvider<API_Login>(requestClosure: login_timeoutClosure)

extension API_Login: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .appleLogin(_, _):
            return "/shield/login"
        case .facebookLogin(_, _, _, _, _):
            return "/shield/login"
        case .googleLogin(_, _, _, _, _):
            return "/shield/login"
        case .events(_, _):
            return "/Events/uoloadEvents"
        case .readReport(_, _):
            return "/Shield/readTime"
        case .checkUser:
            return "/Article/checkUser"
        case .pasteboard(_):
            return ""
        case .attribution(_, _):
            return "/Business/attribution"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .appleLogin(let userId, let appleId):
            ps["userName"] = userId
            ps["identityToken"] = appleId
        case .facebookLogin(let token, let userId, let userName, let email, let headUrl):
            ps["fbToken"] = token
            ps["userId"] = userId
            ps["userName"] = userName
            ps["email"] = email
            ps["headUrl"] = headUrl
        case .googleLogin(let token, let userId, let userName, let email, let headUrl):
            ps["ggToken"] = token
            ps["userId"] = userId
            ps["userName"] = userName
            ps["email"] = email
            ps["headUrl"] = headUrl
        case .events(let event, let time):
            ps["event"] = event
            ps["time"] = time
            ps["createTime"] = Date().timeStamp
        case .readReport(let bookId, let duration):
            ps["bookId"] = bookId
            ps["time"] = duration
        case .checkUser:
            break
        case .pasteboard(let query):
            ps["query"] = query
        case .attribution(let launchId, let userId):
            ps["launchId"] = launchId
            ps["userId"] = userId
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
